#ifndef _MJCTR_H_
#define	_MJCTR_H_

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "iohandle.h"
#include "rtctime.h"
#include "pid.h"
#include "sys_init.h"
#include "mytype.h"

#ifdef   _MJCTR_MODULE_
#define  MY_EXT
#else
#define  MY_EXT  extern
#endif

#ifdef	__cplusplus
extern "C" {
#endif

#define STRI_REQ_AUTO					0
#define STRI_REQ_ASSIGN 				1

#define STRI_F0_SEL_AIR					0	
#define STRI_F0_SEL_OBJ					1	

#define STRI_PRESS_DUAL_NONE			0	
#define STRI_PRESS_DUAL_PULSE			1	
#define STRI_PRESS_DUAL_SATURATED		2	
#define STRI_PRESS_DUAL_VACUUM			3	

#define STRI_WATO_MODE_STEAM			0	
#define STRI_WATO_MODE_EXTP				1	

#define STRI_DRY_MODE_NONE				0	
#define STRI_DRY_MODE_AIR_IN			1	
#define STRI_DRY_MODE_VACUUM			2	

#define DRY_STEP_PREV					0
#define DRY_STEP_DLY					1
#define DRY_STEP_DRY					2
#define DRY_STEP_COMP					3
#define DRY_STEP_END					50

#define STRI_WARM_MODE_NONE				0	
#define STRI_WARM_MODE_STEAM			1	
#define STRI_WARM_MODE_EXTHT			2	

#define STRI_MAIN_HEAT_AIR		0	

#define STRI_MAIN_HEAT_OBJ_AVG	1	

#define STRI_MAIN_HEAT_OBJ_MIN	2	

#define STRI_MAIN_HEAT_OBJ_SURE	3	

#define STRI_MAIN_HEAT_PRESS	4	

	typedef struct
	{
		ii16 temp;
		ii16 time;
	} stri_porg_type;

	typedef enum
	{
		STRI_STOP_NONE = 0,
		STRI_STOP_KEY = 1,			
		STRI_STOP_WAT = 2,			
		STRI_STOP_GAO_YA = 3,		
		STRI_STOP_GAN_SHAO = 4,		
		STRI_STOP_DOOR_LOCK = 5,	
		STRI_STOP_ELEC_LOCK = 6,	
		STRI_STOP_DOOR_OPEN = 7,	
	} stri_stop_type;
	
	typedef enum
	{
		STRISTEP_IDLE = 0,		
		STRISTEP_CHECK ,		
		STRISTEP_ORDER_TIME ,	
		STRISTEP_ORDER_HEAT ,	
		STRISTEP_PRESSURIZE ,	
		STRISTEP_VACUUM ,  		
		STRISTEP_PRE_VACUUM ,	
		STRISTEP_PRE_HEAT ,		
		STRISTEP_HEAT ,			
		STRISTEP_DELAY ,		
		STRISTEP_PRESS_DO ,		
		STRISTEP_PRESS_UP ,		
		STRISTEP_PRESS_KEEP ,	
		STRISTEP_AUTO_COOL ,	
		STRISTEP_AIROUT ,		
		STRISTEP_WATOUT ,		
		STRISTEP_DRY ,			
		STRISTEP_COMP_COOL ,	
		STRISTEP_WARM ,			
		STRISTEP_COMP ,			
		STRISTEP_END ,			
	} stri_step_type;
	
	typedef struct
	{
		struct
		{
			uu8 start;
			uu8 stop;
		} cmd;

		struct
		{
			ii16 air, obj, ext, press;
			float f_air, f_obj, f_ext, f_press;

			ii16 air_list[32];	
			ii16 obj_list[32];	
			ii16 ext_list[32];	
			uu8 list_index;		
			uu8 list_time_cnt;

			ii16 order;		
			ii16 air_out;	
			ii16 open;		
			ii16 ft;		
			ii16 offset;	
			ii16 max;		
			ii16 above;		
		} temp;
		
		struct
		{
			ii16 temp;		
			ii16 time;		
		} stri;
		
		struct
		{
			uu8 mode;		
			uu8 seg_max;
			uu8 seg_cnt;
			stri_porg_type *tab;
		} prog;
		
		struct 
		{
			uu8 en				: 1;		
			float cs_up;					
			float cs_dn;					
		} kl;

		struct
		{
			ii16 temp;
			ii16 time;		
			uu8 mode;		
		} dry;
		
		struct
		{
			uu8 mode;		
			ii16 temp;
			ii16 time;		
		} warm;
		
		struct
		{
			ii16	temp;			
			ii16	press_offset;	
			uu8	sv;					
			uu8 cnt;				
			uu8 st;     			
			uu8 mode;				
			uu8 saturated_ok;		
		} press_dual;
		
		struct
		{
			uu8 ot;							
			uu8 dly;						
			wt_add_type obj;				
		} wat_add;
		
		struct
		{
			ii16 temp;						
			ii16 time;						
			uu8 mode;						
			uu8 req;						
		} wat_out;
		
		struct
		{
			ii16 buchang_press;				
			ii16 buchang_offset;			
			uu8 check_step;					
			uu8 check_cnt;					
			uu8 check_ok			: 1;	
			uu8 check_fail			: 1;	
			uu8 check_en			: 1;	
			uu8 buchang_en			: 1;	
		} pressurize;
		
		struct 
		{
			uu8 check_en			: 1;	
			uu8 fore_en				: 1;	
			uu8 check_step;					
			uu8 check_cnt;					
		} vacuum;
		
		struct
		{
			ii16 heat_delay_time;			
			
			ii16 order_time_dly;				
			sig_time_type order_time_rtc;		
			ii16 order_heat_timeout_sv;			

			uu8 airout_level;				
			uu8 b_safe_tst			: 1;	
			uu8 order_heat_en		: 1;	
			
			uu8 f0_sel				: 1;	
		} cfg;

		struct 
		{
			uu8 first;
			stri_step_type now;
			stri_step_type old;
			stri_step_type req;
			ii32 time_cnt;
		} step;
		
		struct
		{
			int32_t air_out_en_time;
			int32_t air_out_dis_time; 
			ii32 sub_time_cnt;			
			ii32 total_time_cnt;		
			ii32 heat_time_cnt;			
			float f0_stri, f0_proc;		
			uu8 f0_time_cnt;			
			uu8 dry_step;				
			out_sw_type airout;			
			stri_stop_type stop_src;	
			char sucess			: 1;	
			char wat_add_allow	: 1;	
		} st;
		
		struct
		{
			sig_time_type rtc;			
			ii16	 loop_cnt;			
			
			char wat_low		:1;		
			char wat_high		:1;		
			char pulse_1s       :1;		
			char pulse_500ms    :1;		
			char pulse_100ms	:1;		
		} mix;
		
		struct
		{
			char main_en:1, ext_en:1;				
			char main_mode_set;						
			char main_mode_now, main_mode_old;		
			ii16 main_max, ext_max;					
			ii16 ext_main_offset;					
			ii16 main_period, main_duty;			
			ii16 ext_period,ext_duty;				
			uint16_t pid_t;							
			uint16_t air_pid_tab[3];				
			uint16_t ext_pid_tab[3]; 
			uint16_t obj_pid_tab[3];
			pid_type air_pid;						
			pid_type ext_pid;
			pid_type obj_pid;
			pid_tune_type air_tune, ext_tune, obj_tune;
			float main_sv;							
			float ext_sv;			
		} heat;
		
		struct
		{
			char air_out;					
			char wat_out			: 1;	
			char wat_add			: 1;	
			char air_in_pumb		: 1;	
			char pressurize_pumb	: 1;	
			char vacuum_pumb		: 1;	
			char fan_en_req			: 1;	
			char fan_dis_req		: 1;	
		} out;
	} stri_type;

	typedef struct
	{
		uu8 mode;
		char (* fun)(void);
	} stri_fun_type;	
	
	typedef struct
	{
		stri_fun_type idle;
		stri_fun_type check_self;
		stri_fun_type order_time;
		stri_fun_type order_heat;
		stri_fun_type pressurize;
		stri_fun_type vacuum;
		stri_fun_type for_vacuum;
		stri_fun_type pre_heat;
		stri_fun_type heat;
		stri_fun_type heat_delay;
		stri_fun_type press_dual;
		stri_fun_type press_up;
		stri_fun_type press_keep;
		stri_fun_type auto_cool;
		stri_fun_type air_out;
		stri_fun_type wat_out;
		stri_fun_type dry;
		stri_fun_type warm;
		stri_fun_type comp_cool;
		stri_fun_type comp;
		stri_fun_type end;
	} stri_fun_list_type;
	
	#define STRI_FUN_NOP		0
	#define STRI_FUN_EXT		1
	#define STRI_FUN_ORR		2
	
	typedef struct
	{
		ii16 airout_time;			
		ii16 airout_level_max;		
		char t_obj_en;				
		char t_ext_en;				
		char press_en;				
		char vaccum_en;				
		char pressurize_en;			
		char air_in_en;				
		char wat_add_en;			
		char wat_out_en;			
		char dry_start_delay;		
		ii16 dry_vacuum_low;		
		ii16 dry_vacuum_high;		
		ii16 wat_add_dis_temp;		
		ii16 saturated_delay;		
		ii16 press_pulse_delay;		
		ii16 press_end_temp;		
		ii16 pressur_check_val;	    
		ii16 pressur_check_min;	    
		ii16 pressur_check_time;	
		ii16 vacuum_check_val;	    
		ii16 vacuum_check_max;	    
		ii16 vacuum_check_time;		
	} stri_hal_type;
	
	void stri_start(void);
	void stri_stop(void);
	
	char stri_step_req(stri_step_type req, uu8 mode);
	void stri_heat_main(void);
	void stri_heat_ext(void);
	void stri_config(void);
	void stri_rt_update(void);
	void stri_wat_add_handle(void);
	void stri_main_heat_ctr(char ctr, float sv);
	void stri_ext_heat_ctr(char ctr, float sv);
	
	#define stri_list_index(x)	((x) & 0x1F)

	void stri_control(void);
	
	extern stri_type stri;
	extern const stri_fun_list_type stri_fun;
	
	extern const short stri_airout_period[];	
	extern const stri_hal_type stri_hal;		
	
#ifdef	__cplusplus
}
#endif

#endif	

